//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios

//1 使用axios前置拦截器，让所有的请求都携带uToken
axios.interceptors.request.use(config=>{
    //携带token
    let uToken =  localStorage.getItem("token");
    if(uToken){
        config.headers['U-TOKEN']=uToken;
    }
    return config;
},error => {
    Promise.reject(error);
})


//2 使用axios后置拦截器，处理没有登录请求  统一处理所有的axios请求响应结果
axios.interceptors.response.use(result=>{
    let data = result.data;
    if(!data.success && data.message==="noLogin")
        location.href = "login.html"
    return result;
},error => {
    Promise.reject(error);
})

let url = location.href;
//如果访问的不是登录页面，不是注册页面，不是首页，并且localstorage中没有登录信息，那么就跳转到登录页面
if(url.indexOf("/login") === -1 && url.indexOf("/register") === -1 && url.indexOf("/index") === -1
&& url.indexOf("/callback") === -1 && url.indexOf("/binder") === -1){
    let user = JSON.parse(localStorage.getItem("logininfo"));
    if(!user){
        location.href = "login.html"
    }
}
function parseUrlParams2Obj(url){//http://bugtracker.itsource.cn/?code=071m46ll2v5Rv94Hdxnl2Ux7ZB1m46lh&state=1
    let paramStr = url.substring(url.indexOf("?")+1);//code=071m46ll2v5Rv94Hdxnl2Ux7ZB1m46lh&state=1
    let paramStrs = paramStr.split("&");//[code=071m46ll2v5Rv94Hdxnl2Ux7ZB1m46lh,state=1]
    let paramObj = {};
    for(let i=0; i<paramStrs.length; i++){
        let paramName = paramStrs[i].split("=")[0];//code = 071m46ll2v5Rv94Hdxnl2Ux7ZB1m46lh
        let paramValue = paramStrs[i].split("=")[1];
        paramObj[paramName] = paramValue;
    }
    return paramObj
}